package com.smyh.UIM.device;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.smyh.UIM.MainActivity;
import com.smyh.UIM.R;
import com.smyh.bean.DeviceParam3;
import com.smyh.cache.DeviceParamInfoCache;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.commcache.commd.UIParamOptionUtil;
import com.smyh.common.MinaClientThread;
import com.smyh.protcol.device.CMDUtil;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;

/**
 * Created by Administrator on 2016/7/27.
 */
public class DeviceParam03Fm extends Fragment implements View.OnClickListener {
    //电池电量低告警使能
    //电池电量低告警定值
    //防误动10MS使能
    //DMA与全通道切换(1字节)
    //电压变化百分比：录波
    //故障电流录波自动恢复时间:两个字节
    //暂态法使能:1个字节
    //暂态法电流突变(0.1A)
    //暂态法电压启动值
    //D13-14-录播电流触发突变值
    //D15-16-录播电场启动值
    //D17-18-录播电场变化百分比：

    private static TextView batteryLowWarn,batteryLowWarnValue,defendErrorAction10MSValue,DMAChangeAllChanel,voltChangePersent,
            faultElecWaveResetTime,tempAble,tempHarryChange,tempVoltStartValue,recordElecBreakValue,
            recordElecFildStartValue,recordElecFildPercentValue;
    private Button param3ReadBtn;

    public Handler handler;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_device_param03,container,false);

        initView(view);
        initEvent();

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };
        return view;
    }

    private void initEvent() {
        batteryLowWarn.setOnClickListener(this);
        batteryLowWarnValue.setOnClickListener(this);
        defendErrorAction10MSValue.setOnClickListener(this);
        DMAChangeAllChanel.setOnClickListener(this);
        voltChangePersent.setOnClickListener(this);
        faultElecWaveResetTime.setOnClickListener(this);
        tempAble.setOnClickListener(this);
        tempHarryChange.setOnClickListener(this);
        tempVoltStartValue.setOnClickListener(this);
        recordElecBreakValue.setOnClickListener(this);
        recordElecFildStartValue.setOnClickListener(this);
        recordElecFildPercentValue.setOnClickListener(this);

        param3ReadBtn.setOnClickListener(btnClickListenrer);
    }

    private void initView(View view) {
        batteryLowWarn = (TextView) view.findViewById(R.id.batteryLowWarn);
        batteryLowWarnValue = (TextView) view.findViewById(R.id.batteryLowWarnValue);
        defendErrorAction10MSValue = (TextView) view.findViewById(R.id.defendErrorAction10MSValue);
        DMAChangeAllChanel = (TextView) view.findViewById(R.id.DMAChangeAllChanel);
        voltChangePersent = (TextView) view.findViewById(R.id.voltChangePersent);
        faultElecWaveResetTime = (TextView) view.findViewById(R.id.faultElecWaveResetTime);
        tempAble = (TextView) view.findViewById(R.id.tempAble);
        tempHarryChange = (TextView) view.findViewById(R.id.tempHarryChange);
        tempVoltStartValue = (TextView) view.findViewById(R.id.tempVoltStartValue);
        recordElecBreakValue = (TextView) view.findViewById(R.id.recordElecBreakValue);
        recordElecFildStartValue = (TextView) view.findViewById(R.id.recordElecFildStartValue);
        recordElecFildPercentValue = (TextView) view.findViewById(R.id.recordElecFildPercentValue);

        param3ReadBtn = (Button) view.findViewById(R.id.param3ReadBtn);
    }
    public static void freshDeviceParam3Model() {
        handler1.sendMessage(new Message());

    }
    static Handler handler1 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            DeviceParam3 obj = DeviceParamInfoCache.getDeviceParamMap3().get(MainCahce.chooseDeviceAddr);
            if (obj != null){

                batteryLowWarn.setText(obj.getBatteryLowWarn()+"");
                batteryLowWarnValue.setText(obj.getBatteryLowWarnValue()+"");
                defendErrorAction10MSValue.setText(obj.getDefendErrorAction10MSValue()+"");
                DMAChangeAllChanel.setText(obj.getDMAChangeAllChanel()+"");
                voltChangePersent.setText(obj.getVoltChangePersent()+"");
                faultElecWaveResetTime.setText(obj.getFaultElecWaveResetTime()+"");
                tempAble.setText(obj.getTempAble()+"");
                tempHarryChange.setText(obj.getTempHarryChange()+"");
                tempVoltStartValue.setText(obj.getTempVoltStartValue()+"");
                recordElecBreakValue.setText(obj.getRecordElecBreakValue()+"");
                recordElecFildStartValue.setText(obj.getRecordElecFildStartValue()+"");
                recordElecFildPercentValue.setText(obj.getRecordElecFildPercentValue()+"");
            }else {
                batteryLowWarn.setText(" ");
                batteryLowWarnValue.setText( " ");
                defendErrorAction10MSValue.setText( " ");
                DMAChangeAllChanel.setText( " ");
                voltChangePersent.setText( " ");
                faultElecWaveResetTime.setText( " ");
                tempAble.setText( " ");
                tempHarryChange.setText( " ");
                tempVoltStartValue.setText( " ");
                recordElecBreakValue.setText( " ");
                recordElecFildStartValue.setText( " ");
                recordElecFildPercentValue.setText(" ");

            }
        }
    };
    private View.OnClickListener btnClickListenrer = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.param3ReadBtn:{
                    if(MinaClientThread.session == null){
                        Toast.makeText(getContext(),"请连接wifi！",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    IoBuffer buf = CMDUtil.askOutParam(HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr), (byte) 3);
                    MinaClientThread.session.write(buf);
                    Toast.makeText(getContext(),"读取数据！",Toast.LENGTH_SHORT).show();
                    break;
                }
                default:{
                    break;
                }
            }
        }
    };
    int index = 1;
    String str = "";
    @Override
    public void onClick(View v) {
        if(MinaClientThread.session==null){
            Toast.makeText(getContext(), "请连接wifi", Toast.LENGTH_SHORT).show();
            return;
        }
        if(MainCahce.chooseDeviceAddr.equals("")){
            Toast.makeText(getContext(), "请选择设备", Toast.LENGTH_SHORT).show();
            return;
        }
        switch (v.getId()){
            case R.id.batteryLowWarn:{
                index = 1;
                str = batteryLowWarn.getText().toString();
                break;
            }
            case R.id.batteryLowWarnValue:{
                index = 2;
                str = batteryLowWarnValue.getText().toString();
                break;
            }
            case R.id.defendErrorAction10MSValue:{
                index = 3;
                str = defendErrorAction10MSValue.getText().toString();
                break;
            }
            case R.id.DMAChangeAllChanel:{
                index = 4;
                str = DMAChangeAllChanel.getText().toString();
                break;
            }case R.id.voltChangePersent:{
                index = 5;
                str = voltChangePersent.getText().toString();
                break;
            }case R.id.faultElecWaveResetTime:{
                index = 6;
                str = faultElecWaveResetTime.getText().toString();
                break;
            }case R.id.tempAble:{
                index = 7;
                str = tempAble.getText().toString();
                break;
            }case R.id.tempHarryChange:{
                index = 8;
                str = tempHarryChange.getText().toString();
                break;
            }
            case R.id.tempVoltStartValue:{
                index = 9;
                str = tempVoltStartValue.getText().toString();
                break;
            }
            case R.id.recordElecBreakValue:{
                index = 10;
                str = recordElecBreakValue.getText().toString();
                break;
            }case R.id.recordElecFildStartValue:{
                index = 11;
                str = recordElecFildStartValue.getText().toString();
                break;
            }case R.id.recordElecFildPercentValue:{
                index = 12;
                str = recordElecFildPercentValue.getText().toString();
                break;
            }
        }
        final EditText edit = new EditText(getContext());
        edit.setText(str);
        edit.setSelection(str.length());
        new AlertDialog.Builder(getContext()).setTitle("请输入").setView(edit)
                .setPositiveButton("修改", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            byte[] addrs = HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr);
                            IoBuffer buffer = UIParamOptionUtil.writeDeviceParm(addrs,3,index,Integer.valueOf(edit.getText().toString()));
                            MinaClientThread.session.write(buffer);
                        }catch (Exception e){
                            Toast.makeText(getContext(), "格式不正确", Toast.LENGTH_SHORT).show();
                        }
                    }
                }) .setNegativeButton("取消", null).show();
    }

}
